import 'package:flutter/material.dart';
import 'package:loxen/api/http_server/report.dart';
import 'package:loxen/dialog/toast.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class ContentWidget extends StatelessWidget with ReportMixin {
  final String name;
  final String type;
  final String id;
  final ReportType reportType;

  const ContentWidget(
    this.name,
    this.type,
    this.id,
    this.reportType, {
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ValueNotifier<bool> content = ValueNotifier(false);
    TextEditingController controller = TextEditingController();
    controller.addListener(() {
      if (controller.text.trim() == '') {
        content.value = false;
      } else {
        content.value = true;
      }
    });
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(double.infinity, 95.h),
        child: Container(
          color: Theme.of(context).backgroundColor,
          child: SafeArea(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    MySubassembly.getBackIcon(context,
                        color: Theme.of(context).textTheme.subtitle2.color),
                    Expanded(
                      child: Center(
                        child: Text(
                          name,
                          style: TextStyle(
                              fontSize: 32.sp,
                              fontWeight: FontWeight.w500,
                              color:
                                  Theme.of(context).textTheme.subtitle2.color),
                        ),
                      ),
                    ),
                    MySubassembly.getSearchIcon(null, text: Constants.empty),
                  ],
                )
              ],
            ),
          ),
        ),
      ),
      body: Container(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                    border:
                        Border(bottom: BorderSide(color: Color(0xFFEBECF1)))),
                margin: EdgeInsets.only(left: 94.w, right: 94.w, top: 46.h),
                padding: EdgeInsets.only(bottom: 20.h),
                child: TextField(
                  controller: controller,
                  keyboardType: TextInputType.multiline,
                  style: TextStyle(
                    fontSize: 24.sp,
                    color: Colors.black,
                  ),
                  decoration: InputDecoration(
                      hintText:
                          '''如果内容或者其他用户侵犯了您的合法权益，请详细 说明侵权内容并留下您的联系方式，我们将尽快和您 联系。''',
                      border: InputBorder.none),
                  maxLines: 20,
                  maxLength: 1000,
                ),
              ),
              ValueListenableProvider.value(
                value: content,
                builder: (context, _) {
                  bool data = context.watch<bool>();
                  return Container(
                    margin: EdgeInsets.only(top: 140.h, bottom: 200.h),
                    width: 559.w,
                    height: 96.h,
                    child: Material(
                      color: data ? Color(0xFF0CABCB) : Color(0xFF696C72),
                      child: InkWell(
                        onTap: data
                            ? () {
                                report(id, type, controller.text, reportType)
                                    .listen((event) {
                                  if (event == 201) {
                                    Toast.show('举报成功', context);
                                    Navigator.pop(context, true);
                                  } else {
                                    Toast.show('举报失败', context);
                                  }
                                }, onError: (error) {
                                  Toast.show(StringResources.wang_luo_yi_chang,
                                      context);
                                });
                              }
                            : null,
                        child: Center(
                          child: Text(
                            '举报',
                            style: TextStyle(
                              fontSize: 30.sp,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
